/*
 * Licensed to The Apereo Foundation under one or more contributor license
 * agreements. See the NOTICE file distributed with this work for additional
 * information regarding copyright ownership.
 *
 *
 * The Apereo Foundation licenses this file to you under the Educational
 * Community License, Version 2.0 (the "License"); you may not use this file
 * except in compliance with the License. You may obtain a copy of the License
 * at:
 *
 *   http://opensource.org/licenses/ecl2.txt
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
 * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.  See the
 * License for the specific language governing permissions and limitations under
 * the License.
 *
 */

package org.opencastproject.execute.operation.handler;

import org.opencastproject.execute.api.ExecuteException;
import org.opencastproject.execute.api.ExecuteService;
import org.opencastproject.inspection.api.MediaInspectionException;
import org.opencastproject.inspection.api.MediaInspectionService;
import org.opencastproject.job.api.Job;
import org.opencastproject.job.api.JobContext;
import org.opencastproject.mediapackage.MediaPackage;
import org.opencastproject.mediapackage.MediaPackageElement;
import org.opencastproject.mediapackage.MediaPackageElementFlavor;
import org.opencastproject.mediapackage.MediaPackageElementParser;
import org.opencastproject.mediapackage.MediaPackageException;
import org.opencastproject.serviceregistry.api.ServiceRegistry;
import org.opencastproject.util.NotFoundException;
import org.opencastproject.workflow.api.AbstractWorkflowOperationHandler;
import org.opencastproject.workflow.api.ConfiguredTagsAndFlavors;
import org.opencastproject.workflow.api.WorkflowInstance;
import org.opencastproject.workflow.api.WorkflowOperationException;
import org.opencastproject.workflow.api.WorkflowOperationHandler;
import org.opencastproject.workflow.api.WorkflowOperationInstance;
import org.opencastproject.workflow.api.WorkflowOperationResult;
import org.opencastproject.workflow.api.WorkflowOperationResult.Action;
import org.opencastproject.workflow.api.WorkflowOperationResultImpl;
import org.opencastproject.workspace.api.Workspace;

import org.apache.commons.lang3.StringUtils;
import org.osgi.service.component.annotations.Component;
import org.osgi.service.component.annotations.Reference;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStreamReader;
import java.net.URI;
import java.nio.charset.StandardCharsets;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Properties;

/**
 * Runs an operation once with using elements within a certain MediaPackage as parameters
 */
@Component(
    immediate = true,
    service = WorkflowOperationHandler.class,
    property = {
        "service.description=Execute Once Workflow Operation Handler",
        "workflow.operation=execute-once"
    }
)
public class ExecuteOnceWorkflowOperationHandler extends AbstractWorkflowOperationHandler {

  /** The logging facility */
  private static final Logger logger = LoggerFactory.getLogger(ExecuteOnceWorkflowOperationHandler.class);

  /** Property containing the command to run */
  public static final String EXEC_PROPERTY = "exec";

  /** Property containing the list of command parameters */
  public static final String PARAMS_PROPERTY = "params";

  /** Property containingn an approximation of the load imposed by running this operation */
  public static final String LOAD_PROPERTY = "load";

  /** Property containing the filename of the elements created by this operation */
  public static final String OUTPUT_FILENAME_PROPERTY = "output-filename";

  /** Property containing the expected type of the element generated by this operation */
  public static final String EXPECTED_TYPE_PROPERTY = "expected-type";

  /** Property containing the flavor that the resulting mediapackage elements will be assigned */
  public static final String TARGET_FLAVOR_PROPERTY = "target-flavor";

  /** Property should be empty, but is needed in order to parse optional target-flavors */
  public static final String TARGET_FLAVORS_PROPERTY = "target-flavors";

  /** Property containing the tags that the resulting mediapackage elements will be assigned */
  public static final String TARGET_TAGS_PROPERTY = "target-tags";

  /** Property should be empty, but will be checked, if target-tags is empty */
  public static final String TARGET_TAG_PROPERTY = "target-tag";

  /** Property to control whether command output will be used to set workflow properties */
  public static final String SET_WF_PROPS_PROPERTY = "set-workflow-properties";

  /** The text analyzer */
  protected ExecuteService executeService;

  /** Reference to the media inspection service */
  private MediaInspectionService inspectionService = null;

  /** The workspace service */
  protected Workspace workspace;

  /**
   * {@inheritDoc}
   *
   * @see org.opencastproject.workflow.api.WorkflowOperationHandler#start(org.opencastproject.workflow.api.WorkflowInstance, JobContext)
   */
  @Override
  public WorkflowOperationResult start(WorkflowInstance workflowInstance, JobContext context) throws WorkflowOperationException {

    MediaPackage mediaPackage = workflowInstance.getMediaPackage();
    WorkflowOperationInstance operation = workflowInstance.getCurrentOperation();

    logger.debug("Running execute workflow operation with ID {}", operation.getId());

    // Get operation parameters
    String exec = StringUtils.trimToNull(operation.getConfiguration(EXEC_PROPERTY));
    String params = StringUtils.trimToNull(operation.getConfiguration(PARAMS_PROPERTY));
    float load = 1.0f;
    String loadPropertyStr = StringUtils.trimToEmpty(operation.getConfiguration(LOAD_PROPERTY));
    if (StringUtils.isNotBlank(loadPropertyStr)) {
      try {
        load = Float.parseFloat(loadPropertyStr);
      } catch (NumberFormatException e) {
        String description = StringUtils.trimToEmpty(operation.getDescription());
        logger.warn("Ignoring invalid load value '{}' on execute operation with description '{}'", loadPropertyStr, description);
      }
    }
    ConfiguredTagsAndFlavors tagsAndFlavors = getTagsAndFlavors(workflowInstance,
        Configuration.none, Configuration.none, Configuration.many, Configuration.many);
    List<MediaPackageElementFlavor> targetFlavorStr = tagsAndFlavors.getTargetFlavors();
    List<String> targetTags = tagsAndFlavors.getTargetTags();
    String outputFilename = StringUtils.trimToNull(operation.getConfiguration(OUTPUT_FILENAME_PROPERTY));
    String expectedTypeStr = StringUtils.trimToNull(operation.getConfiguration(EXPECTED_TYPE_PROPERTY));

    boolean setWfProps = Boolean.valueOf(StringUtils.trimToNull(operation.getConfiguration(SET_WF_PROPS_PROPERTY)));

    // Unmarshall target flavor
    MediaPackageElementFlavor targetFlavor = null;
    if (!targetFlavorStr.isEmpty())
      targetFlavor = targetFlavorStr.get(0);

    // Unmarshall expected mediapackage element type
    MediaPackageElement.Type expectedType = null;
    if (expectedTypeStr != null) {
      for (MediaPackageElement.Type type : MediaPackageElement.Type.values())
        if (type.toString().equalsIgnoreCase(expectedTypeStr)) {
          expectedType = type;
          break;
        }

      if (expectedType == null)
        throw new WorkflowOperationException("'" + expectedTypeStr + "' is not a valid element type");
    }

    // Process the result element
    MediaPackageElement resultElement = null;

    try {
      Job job = executeService.execute(exec, params, mediaPackage, outputFilename, expectedType, load);

      WorkflowOperationResult result = null;

      // Wait for all jobs to be finished
      if (!waitForStatus(job).isSuccess())
        throw new WorkflowOperationException("Execute operation failed");

      if (StringUtils.isNotBlank(job.getPayload())) {

        if (setWfProps) {
          // The job payload is a file with set of properties for the workflow
          resultElement = MediaPackageElementParser.getFromXml(job.getPayload());

          final Properties properties = new Properties();
          File propertiesFile = workspace.get(resultElement.getURI());
          try (InputStreamReader reader = new InputStreamReader(new FileInputStream(propertiesFile), StandardCharsets.UTF_8)) {
            properties.load(reader);
          }
          logger.debug("Loaded {} properties from {}", properties.size(), propertiesFile);
          workspace.deleteFromCollection(ExecuteService.COLLECTION, propertiesFile.getName());

          Map<String, String> wfProps = new HashMap<String, String>((Map) properties);

          result = createResult(mediaPackage, wfProps, Action.CONTINUE, job.getQueueTime());
        } else {
          // The job payload is a new element for the MediaPackage
          resultElement = MediaPackageElementParser.getFromXml(job.getPayload());

          if (resultElement.getElementType() == MediaPackageElement.Type.Track) {
            // Have the track inspected and return the result
            Job inspectionJob = null;
            inspectionJob = inspectionService.inspect(resultElement.getURI());

            if (!waitForStatus(inspectionJob).isSuccess()) {
              throw new ExecuteException("Media inspection of " + resultElement.getURI() + " failed");
            }

            resultElement = MediaPackageElementParser.getFromXml(inspectionJob.getPayload());
          }

          // Store new element to mediaPackage
          mediaPackage.add(resultElement);
          URI uri = workspace.moveTo(resultElement.getURI(), mediaPackage.getIdentifier().toString(),
                  resultElement.getIdentifier(), outputFilename);
          resultElement.setURI(uri);

          // Set new flavor
          if (targetFlavor != null)
            resultElement.setFlavor(targetFlavor);

          // Set new tags
          if (!targetTags.isEmpty()) {
            // Assume the tags starting with "-" means we want to eliminate such tags form the result element
            for (String tag : targetTags) {
              if (tag.startsWith("-"))
                // We remove the tag resulting from stripping all the '-' characters at the beginning of the tag
                resultElement.removeTag(tag.replaceAll("^-+", ""));
              else
                resultElement.addTag(tag);
            }
          }
          result = createResult(mediaPackage, Action.CONTINUE, job.getQueueTime());
        }
      } else {
        // Payload is empty
        result = createResult(mediaPackage, Action.CONTINUE, job.getQueueTime());
      }

      logger.debug("Execute operation {} completed", operation.getId());

      return result;

    } catch (ExecuteException e) {
      throw new WorkflowOperationException(e);
    } catch (MediaPackageException e) {
      throw new WorkflowOperationException("Some result element couldn't be serialized", e);
    } catch (NotFoundException e) {
      throw new WorkflowOperationException("Could not find mediapackage", e);
    } catch (IOException e) {
      throw new WorkflowOperationException("Error unmarshalling a result mediapackage element", e);
    } catch (MediaInspectionException e) {
      throw new WorkflowOperationException("Media inspection of " + resultElement.getURI() + " failed", e);
    }

  }

  /**
   * {@inheritDoc}
   * 
   * @see org.opencastproject.workflow.api.WorkflowOperationHandler#skip(org.opencastproject.workflow.api.WorkflowInstance, JobContext)
   */
  @Override
  public WorkflowOperationResult skip(WorkflowInstance workflowInstance, JobContext context) throws WorkflowOperationException {
    return new WorkflowOperationResultImpl(workflowInstance.getMediaPackage(), null, Action.SKIP, 0);
  }

  @Override
  public String getId() {
    return "execute";
  }

  @Override
  public String getDescription() {
    return "Executes command line workflow operations in workers";
  }

  @Override
  public void destroy(WorkflowInstance workflowInstance, JobContext context) throws WorkflowOperationException {
    // Do nothing (nothing to clean up, the command line program should do this itself)
  }

  /**
   * Sets the service
   * 
   * @param service
   */
  @Reference
  public void setExecuteService(ExecuteService service) {
    this.executeService = service;
  }

  /**
   * Sets a reference to the workspace service.
   * 
   * @param workspace
   */
  @Reference
  public void setWorkspace(Workspace workspace) {
    this.workspace = workspace;
  }

  /**
   * Sets the media inspection service
   * 
   * @param mediaInspectionService
   *          an instance of the media inspection service
   */
  @Reference
  protected void setMediaInspectionService(MediaInspectionService mediaInspectionService) {
    this.inspectionService = mediaInspectionService;
  }

  @Reference
  @Override
  public void setServiceRegistry(ServiceRegistry serviceRegistry) {
    super.setServiceRegistry(serviceRegistry);
  }
}
